package com.pozu.new_retail.service.impl;

import com.pozu.new_retail.dao.mapper.CardDetailMapper;
import com.pozu.new_retail.dao.mapper.CardTagsMapper;
import com.pozu.new_retail.dao.mapper.CardTracesItemMapper;
import com.pozu.new_retail.dao.mapper.GoodsMapper;
import com.pozu.new_retail.dao.model.*;
import com.pozu.new_retail.service.CardDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * 名卡 详情 、聚合接口 service 实现类
 *
 * @author shenxueyuan
 * @Date 2019/3/20
 */
@Service("cardDetailService")
public class CardDetailServiceImpl implements CardDetailService {

    // 名片详情
    @Autowired
    private CardDetailMapper cardDetailMapper;
    // 名片被浏览的 用户记录
    @Autowired
    private CardTracesItemMapper cardTracesUserMapper;
    // 用户贴的 标签
    @Autowired
    private CardTagsMapper cardTagsMapper;
    // 关联商品
    @Autowired
    private GoodsMapper goodsMapper;


    @Override
    public List<CardDetail> queryByMap(HashMap<String, Object> map) {
        return cardDetailMapper.queryByMap(map);
    }

    @Override
    public CardDetail queryByStaffId(int staffId) {
        return cardDetailMapper.queryByStaffId(staffId);
    }

    @Override
    public int insertCardDetail(CardDetail cardDetail) {
        return cardDetailMapper.insertCardDetail(cardDetail);
    }

    @Override
    public int updateCardDetail(CardDetail cardDetail) {
        return cardDetailMapper.updateCardDetail(cardDetail);
    }

    @Override
    public CardProfile queyCardProfile(int appId, int staffId) {

        // 名片详情
        CardProfile cardProfile = new CardProfile();
        CardDetail cardDetail = cardDetailMapper.queryByStaffId(staffId);
        cardProfile.setDetail(cardDetail);

        // 浏览记录 前6条 + 总 浏览数量
        HashMap params = new HashMap();
        params.put("appId", appId);
        params.put("staffId", staffId);
        params.put("type", CardTraceItem.TYPE_TRACE);
        CardTraces cardTraces = new CardTraces();
        List<CardTraceItem> cardTraceUsers = cardTracesUserMapper.queryTracesByMap(params);
        cardTraces.setList(cardTraceUsers);
        // 总的浏览人数
        int traceCnt = cardTracesUserMapper.queryTraceCntByMap(params);
        cardTraces.setCount(traceCnt);
        cardProfile.setTraces(cardTraces);

        // 标签
        List<CardTags> cardTags = cardTagsMapper.queryTagsByMap(params);
        cardProfile.setTags(cardTags);

        // 关联商品
        List<GoodsSimple> goods = goodsMapper.queryGoodsListByIds("'"+cardDetail.getGoodsIds()+"'");
        cardProfile.setRecommendationItem(goods);

        // 版权信息
        cardProfile.setCopyright(new CardCopyright());

        return cardProfile;
    }
}
